package com.js.admin.controller;

import com.js.admin.entity.BaseUser;
import com.js.admin.model.AuthUser;
import com.js.admin.model.FrontUser;
import com.js.admin.service.BaseUserService;
import com.js.admin.service.PermissionService;
import com.js.api.model.PermissionInfo;
import com.js.api.model.UserInfo;
import com.js.auth.client.annotation.CheckUserToken;
import com.js.auth.client.annotation.IgnoreUserToken;
import com.js.common.controller.BaseController;
import com.js.common.response.ObjectRestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author： ygl
 * @date： 2018/10/29
 * @Description：
 */
@Slf4j
@Api(tags = "用户模块")
@CheckUserToken
@RestController
@RequestMapping("/user")
public class BaseUserController extends BaseController<BaseUser,String> {

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private PermissionService permissionService;

    @IgnoreUserToken
    @ApiOperation("账户密码验证")
    @PostMapping(value = "/validate")
    public ObjectRestResponse<UserInfo> validate(String username, String password) {
        log.info("--------------------------------------账户密码验证------------------------------------");
        UserInfo userInfo = new UserInfo();
        userInfo.setId("abc");
        userInfo.setUsername(username);
        return new ObjectRestResponse<UserInfo>().data(userInfo);
//        return new ObjectRestResponse<UserInfo>().data(permissionService.validate(username, password));
    }

    @IgnoreUserToken
    @ApiOperation("根据账户名获取用户信息")
    @PostMapping(value = "/info")
    public ObjectRestResponse<AuthUser> validate(String username) {
        AuthUser user = new AuthUser();
        BaseUser baseUser = baseUserService.getUserByUsername(username);
        BeanUtils.copyProperties(baseUser, user);
        return new ObjectRestResponse<AuthUser>().data(user);
    }

    @ApiOperation("获取用户的信息接口,包含用户权限")
    @GetMapping(value = "/front/info")
    public ResponseEntity<?> getUserInfo() throws Exception {
        FrontUser userInfo = permissionService.getUserInfo();
        if (userInfo == null) {
            return ResponseEntity.status(401).body(false);
        } else {
            return ResponseEntity.ok(userInfo);
        }
    }

    @IgnoreUserToken
    @RequestMapping(value = "/permissions", method = RequestMethod.GET)
    public @ResponseBody
    List<PermissionInfo> getAllPermission(){
        PermissionInfo info = new PermissionInfo();
        info.setUri("/admin/user/{*}");
        List<PermissionInfo> list = new ArrayList<>();
        list.add(info);
        return list;
    }

}
